package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.BlogBiz;
import cn.sumpu.app.guoguojie.constant.BlogClass;
import cn.sumpu.app.guoguojie.constant.BlogVisibility;
import cn.sumpu.app.guoguojie.dao.BlogContentDao;
import cn.sumpu.app.guoguojie.dao.BlogDao;
import cn.sumpu.app.guoguojie.dao.FeedDao;
import cn.sumpu.app.guoguojie.domain.Blog;
import cn.sumpu.app.guoguojie.domain.BlogContent;
import cn.sumpu.app.guoguojie.domain.Ext.BlogExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.PageModel;
import cn.sumpu.app.guoguojie.util.SubstringHTML;

public class BlogBizImpl implements BlogBiz {

	private BlogDao blogDao;
	private BlogContentDao blogContentDao;
	private FeedDao feedDao;

	public void setFeedDao(FeedDao feedDao) {
		this.feedDao = feedDao;
	}

	public void setBlogDao(BlogDao blogDao) {
		this.blogDao = blogDao;
	}

	public void setBlogContentDao(BlogContentDao blogContentDao) {
		this.blogContentDao = blogContentDao;
	}

	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void addBlog(Blog blog) throws AppBusinessException {
		BlogContent blogContent = blog.getBlogContent();
		blogDao.insert(blog);
		blogContentDao.insert(blogContent);
	}

	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void ModifyBlog(Blog blog) throws AppBusinessException {

		blogDao.updateBlog(blog);
		blogContentDao.updateBlogContent(blog.getBlogContent());
	}

	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void DeleteBlog(String blog) throws AppBusinessException {

		blogDao.deleteBlogById(blog);
		blogContentDao.deleteBlogContentByBlog(blog);
		// FIXME 当前删除日志，需要把对应到日志动态删除，以后如果有删除动态机制，则这段代码可去掉
		feedDao.deleteBlogFeed(blog);

	}

	public List<BlogExt> getBlogList(Map<String, Object> map)
			throws AppBusinessException {
		List<BlogExt> bList = blogDao.getBlogList(map);

		return bList;
	}

	public List<BlogContent> getBlogContentList(Map<String, Object> map)
			throws AppBusinessException {
		List<BlogContent> cList = blogContentDao.getBlogContentList(map);
		return cList;
	}

	public Blog GetBlogById(String id) throws AppBusinessException {
		BlogContent blogContent = blogContentDao.getBlogContentById(id);
		Blog blog = blogDao.getBlogById(id);
		blog.setBlogContent(blogContent);
		return blog;
	}

	public PageModel getPageModel(BlogExt blogExt, Integer page,
			String familyType) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		int pageSize = 10;
		//int count = 0;
		map.put("member", blogExt.getMember());
		map.put("classname", blogExt.getClassname());
		Integer startId = (page - 1) * pageSize;

		map.put("startId", startId);
		map.put("pageSize", pageSize);
		List<BlogExt> bList = null;

		if ("friendFamilyType".equals(familyType)) {
			bList = blogDao.getFrientFamilyBlog(map);

		}
		if ("myFamilyType".equals(familyType)) {
			bList = blogDao.getBlogList(map);

		}

		// FIXME 这个循环需要优化
		for (BlogExt tmp : bList) {
			tmp.setClassLabel(BlogClass.getLabelByValue(tmp.getClassname()));
			tmp.setVisibility(BlogVisibility.getLabelByValue(tmp
					.getVisibility()));
			tmp.setSummeryContent(getSummeryContent(tmp.getContent(),
					tmp.getIdentifier()));
		}

		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(bList);
		pm.setPageSize(pageSize);
		return pm;
	}


	// 获取文章内容摘要
	private String getSummeryContent(String content, String blogId) {
		SubstringHTML substringHTML = new SubstringHTML();

		// String summeryContent = substringHTML.subString(100,
		// "...<br><a  href='#' onclick='enterComment(" + blogId
		// + ");'>(阅读全文)</a>");
		String summeryContent = substringHTML.getNoHTMLString(content, 140);
		return summeryContent;
	}

	@Override
	public List<BlogExt> getBlogCountByClass(String member)
			throws AppBusinessException {
		List<BlogExt> list = blogDao.getBlogCountByClass(member);
		// FIXME 这个循环需要优化
		for (BlogExt tmp : list) {
			tmp.setClassLabel(BlogClass.getLabelByValue(tmp.getClassname()));
		}
		return list;
	}

	@Override
	public List<BlogExt> getFrientFamilyBlogCountByClass(String member)
			throws AppBusinessException {
		// FIXME 这个循环需要优化
		List<BlogExt> list=blogDao.getFrientFamilyBlogCountByClass(member);
		for (BlogExt tmp : list) {
			tmp.setClassLabel(BlogClass.getLabelByValue(tmp.getClassname()));
		}
		return list;
	}



}
